import { createRouter, createWebHashHistory } from 'vue-router'
import Login from '../views/Login/Login'
const routes = [
  {
    path: '/',
    name: 'Home',
    component: () => import(/* webpackChunkName: "Home" */ '../views/Home/Home')
  }, {
    path: '/Login',
    name: 'Login',
    component: Login,
    beforeEnter(to, from, next) {
      const { isLogin } = localStorage;
      isLogin ? next({ name: 'Home' }) : next();
    }
  }, {
    path: '/Register',
    name: 'Register',
    component: () => import(/* webpackChunkName: "Register" */ '../views/Register/Register'),
    beforeEnter(to, from, next) {
      const { isLogin } = localStorage;
      isLogin ? next({ name: 'Home' }) : next();
    }
  }, {
    path: '/Shop/:id',
    name: 'Shop',
    component: () => import(/* webpackChunkName: "Shop" */ '../views/Shop/Shop'),
  }, {
    path: '/OrderConfirmation/:id',
    name: 'OrderConfirmation',
    component: () => import(/* webpackChunkName: "OrderConfirmation" */ '../views/OrderConfirmation/OrderConfirmation'),
  }, {
    path: '/CartList',
    name: 'CartList',
    component: () => import(/* webpackChunkName: "CartList" */ '../views/CartList/CartList'),
  }, {
    path: '/OrderList',
    name: 'OrderList',
    component: () => import(/* webpackChunkName: "OrderList" */ '../views/OrderList/OrderList'),
  }, {
    path: '/MyInfo',
    name: 'MyInfo',
    component: () => import(/* webpackChunkName: "MyInfo" */ '../views/MyInfo/MyInfo'),
  }, {
    path: '/MyAddress',
    name: 'MyAddress',
    component: () => import(/* webpackChunkName: "MyAddress" */ '../views/MyInfo/MyAddress'),
  }
  // {
  //   path: '/about',
  //   name: 'About',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  // }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})
router.beforeEach((to, from, next) => {
  const { isLogin } = localStorage;
  const { name } = to;
  let isLoginOrRegister;
  (name === 'Login' || name === 'Register') ? isLoginOrRegister = true : isLoginOrRegister = false;
  // if (name === 'Login' || name === 'Register') {
  //   isLoginOrRegister = true;
  // } else {
  //   isLoginOrRegister = false;
  // }
  (isLogin || isLoginOrRegister) ? next() : next({ name: 'Login' });
})

export default router
